#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer{
private:
    Computer() = default;
#if 0
    Computer(const char * brand,double price)
        : _brand(new char[strlen(brand) + 1]())
          , _price(price)
    {
        strcpy(_brand,brand);
        cout << "Computer(const char *,double)" << endl;
    }
#endif
//禁止复制
    Computer(const Computer & rhs) = delete;

    Computer & operator=(const Computer & rhs) = delete;
public:
    static Computer * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Computer();
        }
        return _pInstance;
    }

    void init(const char * brand,double price){
        if(_brand){
            delete [] _brand;
        }
        _brand = new char[strlen(brand) + 1]();
        strcpy(_brand,brand);
        _price = price;
    }

    void print(){
        cout << "brand:" << _brand << endl
            << "price:" << _price << endl;
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
        cout << "堆对象本身的空间被回收啦~" << endl;
    }
private:
    ~Computer(){
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
        cout << "~Computer()" << endl;
    }
private:
    char * _brand;
    double _price;
    static Computer * _pInstance;
};
Computer * Computer::_pInstance = nullptr;

void test0(){
    Computer::getInstance()->init("ASUS",4500);
    Computer::getInstance()->print();
    Computer::getInstance()->init("APPLE",14500);
    Computer::getInstance()->print();
    Computer::destroy();

}

int main(void){
    test0();
    return 0;
}
